#include "kwdb/client.h"
#include "kwdb/point.h"
#include "kwdb/config.h"
#include "kwdb/status.h"
#include <iostream>
#include <vector>

int main() {
    // Create client configuration
    kwdb::IngestConfig config("127.0.0.1:9090", 5000);

    // Create client
    kwdb::KwdbClient client(config);

    // Example 1: Write single point
    std::cout << "=== Example 1: Single Point Write ===" << std::endl;
    kwdb::Point point1("cpu_metrics");
    point1.add_tag("host", "server01")
          .add_tag("region", "us-west")
          .add_field("usage", 75.5)
          .add_field("temperature", 68.2)
          .timestamp_now();

    kwdb::Status status1 = client.write(point1);
    if (status1.ok()) {
        std::cout << " Single point write succeeded" << std::endl;
    } else {
        std::cout << " Single point write failed: [" << status1.code << "] "
                  << status1.message << std::endl;
    }

    // Example 2: Batch write multiple points
    std::cout << "\n=== Example 2: Batch Write ===" << std::endl;
    std::vector<kwdb::Point> points;

    for (int i = 0; i < 5; i++) {
        kwdb::Point p("memory_metrics");
        p.add_tag("host", "server0" + std::to_string(i))
         .add_tag("region", "us-east")
         .add_field("used_mb", 1024.0 + i * 100)
         .add_field("free_mb", 2048.0 - i * 100)
         .timestamp_now();
        points.push_back(p);
    }

    kwdb::Status status2 = client.batch_write(points);
    if (status2.ok()) {
        std::cout << " Batch write of " << points.size() << " points succeeded" << std::endl;
    } else {
        std::cout << " Batch write failed: [" << status2.code << "] "
                  << status2.message << std::endl;
    }

    // Example 3: Error handling
    std::cout << "\n=== Example 3: Error Handling ===" << std::endl;
    kwdb::Point invalid_point;  // No measurement set
    kwdb::Status status3 = client.write(invalid_point);
    if (status3.ok()) {
        std::cout << " Write succeeded" << std::endl;
    } else {
        if (status3.is_transport_error()) {
            std::cout << "Transport error detected: " << status3.message << std::endl;
        } else if (status3.is_server_error()) {
            std::cout << "Server error detected: " << status3.message << std::endl;
        } else {
            std::cout << "Unknown error: " << status3.message << std::endl;
        }
    }

    return 0;
}